package ufrj.devagil.timer;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Timer {

    String 	description;
    Chronometer	myChronometer;
    long 	tempoTotal, seasonStart, seasonEnd;
    int		id;
    boolean	isRunning;
    
    
    public Timer(int id, String description, Context c)
    {
	this.id 		= id;
	this.description 	= description;
	this.tempoTotal		= 0;
	this.seasonStart	= 0;
	this.seasonEnd		= 0;
	isRunning		= false;
    }
    
    public void setTimerName(String novoNome)
    {
	this.description = novoNome;
    }
    
    public String getTimerName()
    {
	return this.description;
    }
    
    public static Boolean renomear(final Context ctx)
    {
	TimerdeAtividadesHelper bdHelper = new TimerdeAtividadesHelper(ctx);
	AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
	List<String> lista = new ArrayList<String>();
	
	//POG
	final Timer timerSelecionado = new Timer(-1, "", ctx);
	
	lista = bdHelper.getTimersNames();
	
	final CharSequence[] cs = lista.toArray(new CharSequence[lista.size()]);
	
	builder.setTitle("Timer to rename:")
                .setSingleChoiceItems(cs, 0, new DialogInterface.OnClickListener() 
                {
		    
		    public void onClick(DialogInterface dialog, int item) 
		    {
			timerSelecionado.setTimerName(cs[item].toString());
		    }
		})
		.setPositiveButton("Rename", new DialogInterface.OnClickListener() 
		{
		    public void onClick(DialogInterface dialog, int id) 
		    {
			showRenameDialog(ctx, timerSelecionado);
		    }
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
		{
		    public void onClick(DialogInterface dialog, int id)
		    {
			dialog.cancel();
		    }
		});
	
	AlertDialog alert = builder.create();
	alert.show();
	
	return true;
    }

    
    public static void showRenameDialog(final Context ctx, Timer timer)
    {
	final String nomeAntigo = timer.getTimerName();
	
	final TimerdeAtividadesHelper bdHelper = new TimerdeAtividadesHelper(ctx);
	AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
	final EditText input = new EditText(ctx);
	
	builder.setTitle("Rename it!")
	       .setMessage("Enter the new name for \"" + nomeAntigo + "\":")
	       .setView(input)
	       .setPositiveButton("Change", new DialogInterface.OnClickListener()
	       {
		   public void onClick(DialogInterface dialog, int which) 
		   {
		       String novoNome = input.getText().toString();
		       bdHelper.renomearTimer(nomeAntigo, novoNome);
		   }
	       })
	       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
	       {
		   public void onClick(DialogInterface dialog, int which) 
		   {
		       dialog.cancel();
		   }
	       })
	       .setCancelable(false);
	
	AlertDialog alert = builder.create();
	alert.show();
    }
    
    public static Boolean deletar(final Context ctx)
    {
	TimerdeAtividadesHelper bdHelper = new TimerdeAtividadesHelper(ctx);
	AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
	
	
	//POG
	final Timer timerSelecionado = new Timer(-1, "", ctx);
	
	
	List<String> lista = new ArrayList<String>();
	lista = bdHelper.getTimersNames();
	
	final CharSequence[] cs = lista.toArray(new CharSequence[lista.size()]);
	
	
	builder.setTitle("Timer to delete:")
                .setSingleChoiceItems(cs, 0, new DialogInterface.OnClickListener() 
                {
		    
		    public void onClick(DialogInterface dialog, int item) 
		    {
			
			timerSelecionado.setTimerName(cs[item].toString());
		    }
		})
		.setPositiveButton("Delete", new DialogInterface.OnClickListener() 
		{
		    public void onClick(DialogInterface dialog, int id) 
		    {
			showDeleteDialog(ctx, timerSelecionado);
		    }
		})
		.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
		{
		    public void onClick(DialogInterface dialog, int id)
		    {
			dialog.cancel();
		    }
		});
	
	AlertDialog alert = builder.create();
	alert.show();
	
	return true;
    }
    
   
    
    public static void showDeleteDialog(final Context ctx, Timer timer)
    {
	final String nomeTimer = timer.getTimerName();
	
	// Are you sure you want to delete <timerName>?
	final String mensagem = "Are you sure you want to delete \"" + nomeTimer + "\"?";
	AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
	final TimerdeAtividadesHelper bdHelper = new TimerdeAtividadesHelper(ctx);
	
	builder.setTitle("Delete it!")
	       .setMessage(mensagem)
	       .setPositiveButton("Delete", new DialogInterface.OnClickListener()
	       {
		   public void onClick(DialogInterface dialog, int which) 
		   {
		       bdHelper.deletarTimer(nomeTimer);
		   }
	       })
	       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
	       {
		   public void onClick(DialogInterface dialog, int which) 
		   {
		       dialog.cancel();
		   }
	       })
	       .setCancelable(false);
	
	AlertDialog alert = builder.create();
	alert.show();
    }
}
